<?php


namespace app\backend\controller\v1;

use app\backend\validate\NavValidate;
use app\common\model\Nav as NavModel;
use think\exception\ValidateException;

class Nav extends BaseController
{
    /**
     * 导航
     *
     * @return \think\Response
     */
    public function index()
    {
        // 轮播表
        $user = new NavModel();
        // where条件
        $where = [];
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 10);
        $sort = ['sort' => 'asc'];
        $field = '*';
        //获取列表
        $list = $user->fetchListByWhere($where, $sort, $field, $page, $pageSize);
        return sucessMsg(200, '导航获取成功！', ['list' => $list, 'total' => count($list)]);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择数据id！');
        }
        $data = input();
        try {
            validate(NavValidate::class)->scene('update')->check($data);
            if (NavModel::update($data, ['id'=>$id])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }
}